from pynput import keyboard, mouse
import time
from colorama import init, Fore, Style

# ~{3uJ<;/~} colorama
init(autoreset=True)

last_event = None

def process_events(current_event, color_code):
    global last_event
    if last_event is not None:
        interval = current_event[1] - last_event[1]
        colored_output = f"{getattr(Fore, color_code)} {current_event[0]},: {interval:.2f} seconds{Style.RESET_ALL}"
        print(colored_output)
    last_event = current_event

def on_key_press(key):
    try:
        key_char = key.char
        if key_char in ['s', 'a', 'd', 'w']:
            timestamp = time.time()
            current_event = (key_char, timestamp)
            process_events(current_event, "WHITE")
        elif key_char == 'q':
            timestamp = time.time()
            current_event = (key_char, timestamp)
            process_events(current_event, "GREEN")
    except AttributeError:
        pass

def on_click(x, y, button, pressed):
    if button == mouse.Button.left and pressed:
        timestamp = time.time()
        current_event = ('lc', timestamp)
        process_events(current_event, "RED")
    if button == mouse.Button.right and pressed:
        timestamp = time.time()
        current_event = ('rc', timestamp)
        process_events(current_event, "RED")    

# ~{IhVC<|EL<`L}Fw~}
keyboard_listener = keyboard.Listener(on_press=on_key_press)
keyboard_listener.start()

# ~{IhVCJs1j<`L}Fw~}
mouse_listener = mouse.Listener(on_click=on_click)
mouse_listener.start()

try:
    # ~{TKPP<`L}Fw#,V15=7"Iz~}KeyboardInterrupt
    keyboard_listener.join()
    mouse_listener.join()

except KeyboardInterrupt:
    pass

finally:
    # ~{9X1U<`L}Fw~}
    keyboard_listener.stop()
    mouse_listener.stop()